from __future__ import print_function
from __future__ import absolute_import

import logging

from . import core

class CLivecodingService(core.CTaskletService):
    Name = 'Livecoding Service'
    Depends = []
    
    def __init__(self, *args, **kwds):
        core.CTaskletService.__init__(self, *args, **kwds)
    
    def TaskletMain(self):
        from livecoding.reloader import StacklessCodeReloader
        app = core.CApp.GetInstance()

        l = logging.getLogger('livecoding')
        cr = StacklessCodeReloader()
        
        cfg = app.ConfigRoot

        if 'Livecoding' in cfg['Core'] and \
           'Directories' in cfg['Core']['Livecoding']:
            for k, v in cfg['Core']['Livecoding']['Directories'].iteritems():
                cr.AddDirectory(k, v)
        
        dispatch = cr.DispatchPendingFileChanges
        sleep = core.Sleep
        while 1:
            dispatch()
            sleep(1.0)